<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\letter\LetterContent;
use app\common\model\letter\LetterExplain;
use app\common\model\letter\LetterMaterial;
use app\common\model\letter\LetterTemplate;
use app\common\model\letter\LetterTemplateCate;

/**
 * 信件相关
 */
class Letter extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function saveLetter()
    {
        // 照片
        $photo = $this->request->param('photo');
        // 制作方式ID
        $method_id = $this->request->param('method_id');
        $photo = $this->request->param('photo');
        $photo = $this->request->param('photo');
    }

    /**
     * 计算信件费用
     * @return void
     * @throws \think\exception\DbException
     */
    public function calculateLetterCost()
    {
        // 0选择照片   1制作方式
        $params['type'] = $this->request->param('type') ?: 0;
        $params['option_photo'] = $this->request->param('option_photo') ?? '';
        // 1普通打印  2人工代写  3手写拍照 4明信片
        $params['method_id'] = $this->request->param('make_method') ?? 4;
        // 制作方式1-2类型下的文字
        $params['characters'] = $this->request->param('characters') ?: '';
        // 制作方式3类型下的手写版照片
        $params['handwriting_photo'] = $this->request->param('handwriting_photo') ?: '';
        $data = calculateLetterCost($params);
        $this->success('计算成功！', $data);

    }

    /**
     * 保存信件内容
     * @return void
     * @throws \think\exception\DbException
     */
    public function saveLetterContent()
    {
        //  0确认  1无需内容
        $type = $this->request->param('type') ?: 0;
        $nickname = $this->request->param('nickname') ?? '';
        // $content = $this->request->param('content') ?? '';
        // 2025-03-03 修复获取不到完整信息问题
        $content = $this->request->param('content', '', 'trim') ?? ''; // 去除首尾空格
        $signature = $this->request->param('signature') ?? '';
        $date = $this->request->param('date') ?? date('Y年m月d日', time());
        $diary_ids = $this->request->param('diary_ids') ?? '';
        if (!$type) {
            if (!$nickname)
                $this->error('请输入昵称');
        } else {
            $content = '';
        }
// var_dump($content,$this->request->post('content'),urldecode($content));exit;
        $uid = $this->auth->id;
        $info = LetterContent::get(['uid' => $uid, 'letter_id' => 0]);
        if (!$info) {
            $re = LetterContent::create([
                'uid' => $this->auth->id,
                'nickname' => $nickname,
                'content' => $content,
                'signature' => $signature,
                'date' => $date,
                'diary_ids' => $diary_ids,
            ]);
            if (!$re) {
                $this->error('保存失败，请稍后再试~', 0);
            }
            $this->success('保存成功！', $re->id);
        } else {
            $info->nickname = $nickname;
            $info->content = $content;
            $info->signature = $signature;
            $info->date = $date;
            $info->diary_ids = $diary_ids;
            $info->save();
            $this->success('保存成功！', $info->id);
        }
    }

    /**
     * 获取信件说明列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLetterExplainLst()
    {
        $type = $this->request->param('type') ?? -1;
        $list = LetterExplain::field('id,name,desc,type')
            ->where(function ($query) use ($type) {
                if ($type > -1)
                    $query->where(['type' => $type]);
            })
            ->order('id', 'desc')
            ->select();
        $this->success('获取成功！', $list);
    }

    /**
     * 获取信件说明详情
     * @return void
     * @throws \think\exception\DbException
     */
    public function getLetterExplainInfo()
    {
        $e_id = $this->request->param('e_id');
        $info = LetterExplain::where(['id' => $e_id])->field('id,name,desc,type,operation_explain,eject_explain')->find();
        if (!$info)
            $this->error('数据不存在~');
        $this->success('获取成功！', $info);
    }

    /**
     * 获取信件模板分类
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLetterTemplateCate()
    {
        $list = LetterTemplateCate::where(['status' => 1])->field('id,name')->order('id', 'desc')->select();
        unshift($list);
        $this->success('获取成功！', $list);
    }

    /**
     * 获取信件模板
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLetterTemplate()
    {
        $c_id = $this->request->param('c_id') ?? 0;
        $list = LetterTemplate::with(['cate'])->where(function ($query) use ($c_id) {
            if ($c_id)
                $query->where(['cate_id' => $c_id]);
        })->field('id,name,cate_id,image,is_pay,price')->order('sort', 'desc')->select();
        $this->success('获取成功！', $list);
    }

    /**
     * 获取信件材料
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLetterMaterial()
    {
        $page = $this->request->param('page') ?: 1;
        $limit = $this->request->param('limit') ?: 10;
        $list = LetterMaterial::where(['status' => 1])
            ->where('stock', '>', 0)
            ->field('id,image,name,desc,price,stock,is_limit,limit_num')
            ->order('id', 'desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        $this->success('获取成功！', $list);
    }
}
